import Vue from 'vue'
import App from './App.vue'
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import router from './router'
import store from './store'
import axios from "axios";
import VueRouter from "vue-router"


Vue.config.productionTip = false

Vue.use(ElementUI);
Vue.use(VueRouter)


Vue.prototype.$http = axios

axios.interceptors.request.use(config => {
    var token = sessionStorage.getItem("Authorization")
    config.headers['Authorization'] = token;
    return config;
}, error => {
    return Promise.reject(error);
});

const originalPush = VueRouter.prototype.push

VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
}
router.beforeEach((to, from, next) => {
    var token = sessionStorage.getItem("Authorization")
    console.log("token = " + token);
    if (token === null && to.path !== '/' && to.fullPath !== '/' && !to.fullPath.startsWith('/shop') ) {
        next({path: '/'});
    } else {
        next();
    }

});
router.onError((error) => {
    if (error.name === 'NavigationDuplicated') {
        // 忽略NavigationDuplicated错误
    } else {
        // 处理其他类型的错误
        throw error;
    }
});


new Vue({
    render: h => h(App),
    router,
    store,
}).$mount('#app')
